﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.UI;
using AG.Editor.MapUI.Windows;
using AG.Editor.Core.Data;
using AG.Editor.Core;
using AG.Editor.MapUI.Controls;

namespace AG.Editor.MapUI
{
    public partial class AGMapMgrPanel : UserControl, IAGEMainComponent
    {
        AGEMapPanel _ctlMapPanel;

        public AGMapMgrPanel()
        {
            InitializeComponent();

            _ctlMapPanel = new AGEMapPanel();
            splitContainer1.Panel2.Controls.Add(_ctlMapPanel);
            splitContainer1.Panel2.AutoScroll = true;
        }

        public void OnActived(AGEMainMenuMidiator mmm)
        {
            mmm.Clear();

            ToolStripMenuItem mFile = mmm.AddMenu(new ToolStripMenuItem("文件"));
            ToolStripMenuItem miSaveProject = new ToolStripMenuItem("保存项目");
            miSaveProject.Click += new EventHandler(miSaveProject_Click);
            mFile.DropDownItems.Add(miSaveProject);
            ToolStripMenuItem miClose = new ToolStripMenuItem("关闭");
            //miClose.Click += new EventHandler(miClose_Click);
            mFile.DropDownItems.Add(miClose);

            ToolStripMenuItem m2 = mmm.AddMenu(new ToolStripMenuItem("发布"));
            ToolStripMenuItem miPublishModel = new ToolStripMenuItem("发布模型");
            //miPublishModel.Click += new EventHandler(miPublishModel_Click);
            m2.DropDownItems.Add(miPublishModel);

            ToolStripMenuItem m1 = mmm.AddMenu(new ToolStripMenuItem("地图管理"));
            ToolStripMenuItem miCreateMap = new ToolStripMenuItem("创建地图");
            miCreateMap.Click += new EventHandler(miCreateMap_Click);
            m1.DropDownItems.Add(miCreateMap);

            /*
            ToolStripMenuItem miEditModel = new ToolStripMenuItem("修改模型");
            miEditModel.Click += new EventHandler(miEditModel_Click);
            m1.DropDownItems.Add(miEditModel);

            ToolStripMenuItem miRemoveModel = new ToolStripMenuItem("删除模型");
            miRemoveModel.Click += new EventHandler(miRemoveModel_Click);
            m1.DropDownItems.Add(miRemoveModel);
            */
            BindMapTree();
        }

        void miSaveProject_Click(object sender, EventArgs e)
        {
            AGECommonCommand.SaveProject();
            MessageBox.Show("保存完毕", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        void miCreateMap_Click(object sender, EventArgs e)
        {
            AGECreateMapWindow window = new AGECreateMapWindow();
            if (window.ShowDialog() == DialogResult.OK)
            {
                AGMap map = window.Map;
                AGEContext.Current.EProject.AddMap(map);
                AGTools.Current.MapStore.SaveMap(AGEContext.Current.EProject, map);
                BindMapTree();
            }
        }

        private void BindMapTree()
        {
            _ctlTreeMap.Nodes.Clear();
            for (int index = 0; index < AGEContext.Current.EProject.Maps.Count; index++)
            {
                AGMapRef mapRef = AGEContext.Current.EProject.Maps[index];
                TreeNode node = new TreeNode();
                node.Text = mapRef.MapName;
                node.Tag = mapRef;
                _ctlTreeMap.Nodes.Add(node);
            }
        }

        private void _ctlTreeMap_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node != null)
            {
                AGMapRef mapRef = e.Node.Tag as AGMapRef;
                if (mapRef != null)
                {
                    AGMap map = AGTools.Current.MapStore.LoadMap(AGEContext.Current.EProject, mapRef);
                    _ctlMapPanel.SetMap(map);
                }
            }
        }
    }
}
